var game;
window.onload = function() {
	game = new Game();
	game.init();
	document.getElementById('startBtn').onclick = function() {
		game.start();
	}
	document.getElementById('stopBtn').onclick = function() {
		game.stop();
	}
}

var enemyTypeSmall = 0;
var enemyTypeNormal = 1;
var enemyTypeGaint = 2;

var planeWidth = 62;
var planeHeight = 68;

var bloodSupply = 0;
var weaponSupply = 1;

var DIR_UP = 3;
var DIR_DOWN = 4;
var DIR_LEFT = 5;
var DIR_RIGHT = 6;

Game = function() {
	this.canvas = document.getElementById('doit');
	this.ctx = this.canvas.getContext('2d');

	this.canvas.width = 480;
	this.canvas.height = 700;

	this.width = this.canvas.width;
	this.height = this.canvas.height;

	this.speed = 5;
}

Game.prototype.init = function() {
	//initial view
	console.log('init...');
	this.ctx.clearRect(0, 0, this.width, this.height);

	this.timer = new Game.timer(50);

	var game = this;
	var img = new Image();
	img.src = './images/start.png';
	img.onload = function() {
		game.ctx.drawImage(img, 40, 100, 357, 196);
	}

	this.ctx.font = "30px Verdana";
	this.ctx.strokeText('By MachineOrMan', 200, 350);

	//read highest score
	this.score = localStorage.score || 0;
	this.ctx.font = "20px";
	this.ctx.strokeText('Highest Score:', 50, 600);
	this.ctx.font = "";
	this.ctx.strokeText(this.score, 150, 650);

	this.setEvent();
}

Game.prototype.start = function() {

	this.isAlive = true;

	this.enemy = [];
	this.supply = [];
	this.missile = [];
	//create a plane
	this.plane = new Game.plane((this.width - planeWidth) / 2, (this.height - planeHeight));
	//generate enemy over time
	this.genEnemy();
	//generate supply over time
	this.genSupply();
	if (this.isAlive) {
		this.refresh();
	}
}

Game.prototype.draw = function() {
	//draw life
	this.ctx.font = "5px Verdana";
	this.ctx.strokeText('SCORE: ' + this.score, 10, 650);
	this.ctx.strokeText('LIFE: ' + this.plane.life, 10, 680);
	//draw the plane
	this.ctx.drawImage(this.plane.img,
		this.plane.x,
		this.plane.y,
		this.plane.width,
		this.plane.height);
	//draw enemy
	for (var i in this.enemy) {
		if (this.plane.hit(this.enemy[i])) {
			alert('Game Over');
			if(this.score>localStorage.score){
				localStorage.score = this.score;	
			}
			this.stop();
		}
		this.ctx.drawImage(this.enemy[i].img,
			this.enemy[i].x,
			this.enemy[i].y,
			this.enemy[i].width,
			this.enemy[i].height);
	}
	//draw missile
	//draw supply
	for (var i in this.supply) {
		this.ctx.drawImage(this.supply[i].img,
			this.supply[i].x,
			this.supply[i].y,
			this.supply[i].width,
			this.supply[i].height);
	}
	//draw Explosion
}

Game.prototype.refresh = function() {
	//draw by a specific time
	var game = this;
	this.globalT = setInterval(function() {
		for (var i in game.enemy) {
			if (game.spriteDownRange(game.enemy[i])) {
				game.enemy.splice(i, 1);
				i--;
			} else {
				game.enemy[i].move();
			}
		}
		for (var i in game.supply) {
			if (game.spriteDownRange(game.supply[i])) {
				game.supply.splice(i, 1);
				i--;
			} else {
				game.supply[i].move();
			}
		}
		game.ctx.clearRect(0, 0, game.width, game.height);
		game.draw();
	}, this.timer.tick);
}

Game.prototype.setEvent = function() {
	//deal with keyboard event
	var game = this;
	window.addEventListener('keydown', function(e) {
		switch (e.keyCode) {
			case 37: //left
				game.plane.x -= 5;
				break;
			case 38: //up
				game.plane.y -= 5;
				break;
			case 39: //right
				game.plane.x += 5;
				break;
			case 40: //down
				game.plane.y += 5;
				break;
			case 32: //space
				break;
		}
	}, true);
	this.canvas.focus();
}

Game.prototype.stop = function() {
	this.isAlive = false;
	clearInterval(this.globalT);
	clearInterval(this.enemyT);
	clearInterval(this.supplyT);
	this.enemy=[];
	this.supply=[];
	this.missile = [];
	// clearInterval(this.planeT);
	this.init();
}

Game.prototype.spriteDownRange = function(sprite) {
	//sprite go down the canvas, remove it
	return sprite.y > this.height;
}

Game.prototype.genEnemy = function() {
	var game = this;
	var interval = function() {
		return setTimeout(function() {
			game.enemy.push(new Game.enemy(game.width));
			interval();
		}, Math.random() * game.speed * 500);
	}
	this.enemyT = interval();
}

Game.prototype.genSupply = function() {
	var game = this;
	var interval = function() {
		return setTimeout(function() {
			game.supply.push(new Game.supply(game.width));
			interval();
		}, Math.random() * game.speed * 5000);
	}
	this.supplyT = interval();
}